﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Data.SqlClient;
using ThongTin;
using DuLieu;
using ThaoTac;

namespace GiaoDien
{
    public partial class frmDanhSachLopHoc : Form
    {
        public int DongChon;
      
        public string ChucNang = null;
        Lop_ThT lop = new Lop_ThT();
        Lop_DL cls_Lop = new Lop_DL();
        Lop_TT Lop = new Lop_TT();
        public frmDanhSachLopHoc()
        {
            InitializeComponent();
           
        }

        private void frmDanhSachLopHoc_Load(object sender, EventArgs e)
        {

            tblDanhSachLopHoc.DataSource = lop.DanhSachLopHoc();
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            ChucNang = "Them";
            frmQuanLyLopHoc tt = new frmQuanLyLopHoc(ChucNang, Lop);
            tt.addNew += new EventHandler(add);
            tt.Show();
        }
        public void add(Object sender, EventArgs e)
        {
            if (ChucNang.Equals("Them"))
            {
                frmQuanLyLopHoc tt = (frmQuanLyLopHoc)sender;
                DataTable dt = (DataTable)tblDanhSachLopHoc.DataSource;
                dt.Rows.Add(tt.MaLop, tt.TenLop, tt.KhoaHoc, tt.HeDaoTao, tt.Nganh);
                tblDanhSachLopHoc.DataSource = dt;
                Lop.MaHeDaoTao = tt.HeDaoTao;
                Lop.MaKhoaHoc = tt.KhoaHoc;
                Lop.MaLop = tt.MaLop;
                Lop.MaNganh = tt.Nganh;
                Lop.TenLop = tt.TenLop;
                cls_Lop.ThemLopHoc(Lop);
            }
            if (ChucNang.Equals("Sua"))
            {
                frmQuanLyLopHoc tt = (frmQuanLyLopHoc)sender;
                DataTable dt = (DataTable)tblDanhSachLopHoc.DataSource;
                Lop.MaHeDaoTao = tt.HeDaoTao;
                Lop.MaKhoaHoc = tt.KhoaHoc;
                Lop.MaLop = tt.MaLop;
                Lop.MaNganh = tt.Nganh;
                Lop.TenLop = tt.TenLop;
                cls_Lop.SuaThongTinLopHoc(Lop);

                //load ve bang
                tblDanhSachLopHoc.Rows[DongChon].Cells[3].Value = Lop.MaHeDaoTao;
                tblDanhSachLopHoc.Rows[DongChon].Cells[2].Value = Lop.MaKhoaHoc;
                tblDanhSachLopHoc.Rows[DongChon].Cells[0].Value =  Lop.MaLop;
                tblDanhSachLopHoc.Rows[DongChon].Cells[4].Value = Lop.MaNganh;
                tblDanhSachLopHoc.Rows[DongChon].Cells[1].Value = Lop.TenLop;
            }
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            ChucNang = "Sua";
            Lop.MaHeDaoTao = tblDanhSachLopHoc.Rows[DongChon].Cells[3].Value.ToString();
            Lop.MaKhoaHoc = tblDanhSachLopHoc.Rows[DongChon].Cells[2].Value.ToString();
            Lop.MaLop = tblDanhSachLopHoc.Rows[DongChon].Cells[0].Value.ToString();
            Lop.MaNganh = tblDanhSachLopHoc.Rows[DongChon].Cells[4].Value.ToString();
            Lop.TenLop = tblDanhSachLopHoc.Rows[DongChon].Cells[1].Value.ToString();
            frmQuanLyLopHoc tt = new frmQuanLyLopHoc(ChucNang, Lop);
            tt.addNew += new EventHandler(add);
            tt.Show();
        }

        private void tblDanhSachLopHoc_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            DongChon = e.RowIndex;
        }
        private void XoaLopHoc()
        {
            if (Lop.MaLop == "")
            {
                Lop.MaLop = tblDanhSachLopHoc.Rows[DongChon].Cells[0].Value.ToString();
                if (MessageBox.Show("Bạn có thật sự muốn xóa thông tin lớp có mã " + Lop.MaLop + "", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    try
                    {
                        cls_Lop.XoaLopHoc(Lop);
                        tblDanhSachLopHoc.DataSource = cls_Lop.DanhSachLopHoc();
                    }
                    catch
                    {
                        MessageBox.Show("Không thể xóa dữ liệu này, hãy kiểm tra kết nối!", "Thông báo lỗi.", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }
            else
            {
                MessageBox.Show("Bạn hãy chọn lớp muốn xóa.", "Thông báo.", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        private void btnXoa_Click(object sender, EventArgs e)
        {
           XoaLopHoc();
        }
        private void tbxTimKiem_KeyUp(object sender, KeyEventArgs e)
        {
            Lop.MaLop = tbxTimKiem.Text;
            tblDanhSachLopHoc.DataSource = lop.TimKiemLopHoc(Lop);
        }
    }
}
